<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="姓名" size="small">
            <el-input v-model="searchForm.applyName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="应聘岗位" size="small">
            <el-input v-model="searchForm.station"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>
      <el-button type="primary" size="small" icon="el-icon-upload2" @click="downloadFile">导入</el-button>
      <input type="file" @change="fileChanged" ref="file"/>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.feedbackId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="700px">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
        <el-form-item prop="applyName" label="姓名" size="small" class="item-inline">
          <el-input type="text" v-model="teamRow.applyName"></el-input>
        </el-form-item>
        <el-form-item prop="station" label="应聘岗位" size="small" class="item-inline">
          <el-input type="text" v-model="teamRow.station"></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="性别" size="small" class="item-inline">
          <el-radio-group v-model="teamRow.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="way" label="方式" size="small" class="item-inline">
          <el-select v-model="teamRow.way" placeholder="请选择">
            <el-option
              v-for="item in wayList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="age" label="年龄" size="small" class="item-inline">
          <el-input type="text" v-model="teamRow.age"></el-input>
        </el-form-item>
        <el-form-item prop="source" label="来源" size="small" class="item-inline">
          <el-input type="text" v-model="teamRow.source"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" size="small" class="item-inline">
          <el-input type="text" v-model="teamRow.tel"></el-input>
        </el-form-item>
        <el-form-item label="时间" size="small">
        </el-form-item>
        <el-form-item prop="applyTime" label="日期" size="small" class="item-inline">
          <el-date-picker
            v-model="teamRow.applyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 190px">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="weekDay" label="星期" size="small" class="item-inline">
          <el-select v-model="teamRow.weekDay" placeholder="请选择">
            <el-option
              v-for="item in weekDayList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="约试" size="small">
        </el-form-item>
        <el-form-item label="结果" size="small" class="item-inline">
          <el-select v-model="teamRow.isResult" placeholder="请选择">
            <el-option
              v-for="item in isResultList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="面试时间" size="small" class="item-inline">
          <el-date-picker
            v-model="teamRow.interviewTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
            default-time="12:00:00">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" size="small" class="item-inline">
          <el-select v-model="teamRow.status" placeholder="请选择">
            <el-option
              v-for="item in statusList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" size="small" class="item-inline">
          <el-input type="text" v-model="teamRow.tryRemark"></el-input>
        </el-form-item>
        <el-form-item label="人事初试" size="small" class="item-inline">
          <el-select v-model="teamRow.firstTry" placeholder="请选择">
            <el-option
              v-for="item in tryList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结果" size="small" class="item-inline">
          <el-input type="text" v-model="teamRow.result"></el-input>
        </el-form-item>
        <el-form-item label="用人部门复试" size="small" class="item-inline">
          <el-select v-model="teamRow.repeatTry" placeholder="请选择">
            <el-option
              v-for="item in tryList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属团队" size="small" class="item-inline">
          <!--<el-autocomplete-->
          <!--v-model="teamRow.teamName"-->
          <!--:fetch-suggestions="querySearchTeam"-->
          <!--@select="handleSelectTeam"-->
          <!--placeholder="请输入内容"-->
          <!--&gt;-->
          <!--</el-autocomplete>-->
          <el-input type="text" v-model="teamRow.teamName" disabled></el-input>
        </el-form-item>
        <el-form-item label="终试" size="small" class="item-inline">
          <el-select v-model="teamRow.finalTry" placeholder="请选择">
            <el-option
              v-for="item in tryList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入职" size="small" class="item-inline">
          <el-select v-model="teamRow.isEntry" placeholder="请选择">
            <el-option
              v-for="item in tryList"
              :key="item.value"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注说明" size="small">
          <el-input type="textarea" v-model="teamRow.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addFeedback,
    deleteFeedback,
    downloadFeedback,
    getFeedback,
    selectFeedback,
    updateFeedback,
    selectTeam
  } from '@/api/feedback'
  import myTable from '@/components/myTable'

  import { Message } from 'element-ui'
  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'
  import { isvalidPhone } from '@/utils/validate'

  const sex = function(row, column) {
    return row.sex ? '男' : '女'
  }

  const time = {
    props: ['colConfig'],
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                <span slot-scope="scope" >{{scope.row[item.prop]}}</span>
            </el-table-column>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      const validPhone = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入联系电话'))
        } else if (!isvalidPhone(value)) {
          callback(new Error('请输入正确的11位手机号码'))
        } else {
          callback()
        }
      }
      return {
        colConfigs: [
          { prop: 'applyName', label: '姓名', width: '150', fixed: true },
          { prop: 'sex', label: '性别', width: '100', formatter: sex },
          { prop: 'age', label: '年龄', width: '100' },
          { prop: 'tel', label: '联系电话', width: '150' },
          { prop: 'station', label: '应聘岗位', width: '150' },
          { prop: 'way', label: '方式', width: '150' },
          { prop: 'source', label: '来源', width: '150' },
          {
            label: '时间', width: '300', children: [
              { prop: 'weekDay', label: '星期', width: '150' },
              { prop: 'applyTime', label: '日期', width: '150' }
            ], component: time
          },
          {
            label: '约试', width: '600', children: [
              { prop: 'isResult', label: '邀约结果', width: '150' },
              { prop: 'interviewTime', label: '面试时间', width: '150' },
              { prop: 'status', label: '状态', width: '150' },
              { prop: 'tryRemark', label: '备注', width: '150' }
            ], component: time
          },
          { prop: 'firstTry', label: '人事初试', width: '150' },
          { prop: 'repeatTry', label: '用人部门复试', width: '150' },
          { prop: 'finalTry', label: '终试', width: '150' },
          { prop: 'isEntry', label: '入职', width: '150' },
          { prop: 'result', label: '面试结果', width: '150' },
          { prop: 'teamName', label: '所属团队', width: '150' },
          { prop: 'remark', label: '备注', width: '150' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增招聘反馈',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          applyName: [
            { required: true, message: '请输入姓名', trigger: 'change' }
          ],
          station: [
            { required: true, message: '请输入应聘岗位', trigger: 'change' }
          ],
          sex: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          way: [
            { required: true, message: '请选择方式', trigger: 'change' }
          ],
          age: [
            { required: true, message: '请选择年龄', trigger: 'change' }
          ],
          source: [
            { required: true, message: '请输入来源', trigger: 'change' }
          ],
          tel: [
            { required: true, trigger: 'change', validator: validPhone }
          ],
          applyTime: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ],
          weekDay: [
            { required: true, message: '请输入星期', trigger: 'change' }
          ],
          teamName: [
            { required: true, message: '请输入所属团队', trigger: 'change' }
          ]
        },
        visible: false,
        wayList: [
          { value: '自投' },
          { value: '自搜' },
          { value: '校招' },
          { value: '社招' },
          { value: '介绍' },
          { value: '渠道' }
        ],
        weekDayList: [
          { value: '星期一' },
          { value: '星期二' },
          { value: '星期三' },
          { value: '星期四' },
          { value: '星期五' },
          { value: '星期六' },
          { value: '星期日' }
        ],
        isResultList: [
          { value: '面试' },
          { value: '否' },
          { value: '待考虑' },
          { value: 'PASS' },
          { value: '断线' }
        ],
        statusList: [
          { value: '不合格' },
          { value: '—' },
          { value: '加微信聊' },
          { value: '已发公司信息' },
          { value: '路远' },
          { value: '工作已落实' },
          { value: '求职意愿不符' },
          { value: '不考虑换工作' },
          { value: '薪资未达到要求' },
          { value: '不考虑建筑行业' },
          { value: '只想去甲方单位' },
          { value: '回老家' }
        ],
        tryList: [
          { value: '✔' },
          { value: '—' },
          { value: '×' }
        ],
        subjectionDepartmentList: [
          { value: '行政部' },
          { value: '财务部' },
          { value: '运营部' },
          { value: '总经办' },
          { value: '环境艺术所' }
        ]
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      teamId() {
        return this.$store.state.user.teamId
      },
      teamName() {
        return this.$store.state.user.teamName
      },
      isDirector() {
        return this.$store.state.user.isDirector
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.searchFormList.isDirector = this.isDirector
        this.searchFormList.teamId = this.teamId
        this.listLoading = true
        getFeedback(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增招聘反馈'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.teamRow.isTender = 1
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
        this.$set(this.teamRow, 'teamName', this.teamName)
      },
      editTeam(feedbackId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改招聘反馈'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          feedbackId: feedbackId
        }
        selectFeedback(params).then(response => {
          const data = response.responseBody
          this.teamRow = Object.assign({}, this.teamRow, data)
        })
      },
      submitForm(formName) {
        const params = {
          applyName: this.teamRow.applyName,
          sex: this.teamRow.sex,
          age: this.teamRow.age,
          tel: this.teamRow.tel,
          way: this.teamRow.way,
          station: this.teamRow.station,
          source: this.teamRow.source,
          applyTime: this.teamRow.applyTime,
          weekDay: this.teamRow.weekDay,
          isResult: this.teamRow.isResult,
          interviewTime: this.teamRow.interviewTime,
          status: this.teamRow.status,
          tryRemark: this.teamRow.tryRemark ? this.teamRow.tryRemark : '',
          firstTry: this.teamRow.firstTry,
          repeatTry: this.teamRow.repeatTry,
          finalTry: this.teamRow.finalTry,
          isEntry: this.teamRow.isEntry,
          result: this.teamRow.result ? this.teamRow.result : '',
          teamId: this.teamRow.teamId,
          teamName: this.teamRow.teamName,
          remark: this.teamRow.remark ? this.teamRow.remark : ''
        }
        if (!this.dialogType) {
          params.feedbackId = this.teamRow.feedbackId
        }
        if (this.teamRow.teamId) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if (this.dialogType) {
                addFeedback(params).then(response => {
                  console.log(response)
                  this.outerVisible = false
                  this.fetchData()
                })
              } else {
                updateFeedback(params).then(response => {
                  console.log(response)
                  this.outerVisible = false
                  this.fetchData()
                })
              }
            } else {
              console.log('error submit!!')
              return false
            }
          })
        } else {
          this.$message({
            message: '该团队不存在',
            type: 'warning'
          })
        }
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          feedbackId: scope.row.feedbackId,
          isDel: 1
        }
        deleteFeedback(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      downloadFile() {
        this.$refs.file.click()
      },
      fileChanged() {
        const excel = this.$refs.file.value
        const params = new FormData()
        var reg = /^.*\.(?:xls|xlsx)$/i
        if (!reg.test(excel)) {
          Message({
            message: '请上传excel格式的文件!',
            type: 'error',
            duration: 5 * 1000
          })
          return
        } else {
          params.append('file', this.$refs.file.files[0])
          downloadFeedback(params).then(response => {
            this.fetchData()
          })
          if (this.$refs.file) {
            this.$refs.file.value = ''
          }
        }
      },
      querySearchTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.teamRow.teamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectTeam(item) {
        console.log(item)
        this.$set(this.teamRow, 'teamId', item.teamId)
        this.$set(this.teamRow, 'teamName', item.teamName)
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .item-inline {
      display: inline-block;
      width: 300px;
    }
    input[type="file"] {
      display: none;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.inner-dialog {*/
  /*.item-inline {*/
  /*display: inline-block;*/
  /*width: 300px;*/
  /*}*/
  /*}*/

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
